package com.artisan.admin.controllers;

import com.artisan.admin.model.roleGroupViewModels.RoleGroupViewModel;
import com.artisan.admin.model.versionViewModels.VersionViewModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.roleGroupResultModels.RoleGroupResultModel;
import com.artisan.admin.resultModels.startPageResultModels.StartPageResultModel;
import com.artisan.domain.dto.PageDto;
import com.artisan.domain.dto.RoleGroupDto;
import com.artisan.domain.dto.StartPageDto;
import com.artisan.domain.dto.VersionDto;
import com.artisan.domain.serviceInterface.IRoleGroupService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色组控制器
 */
@RequestMapping("/api/roleGroup")
@RestController
@Api("角色组模块")
@ApiOperation(value = "角色组模块")
public class RoleGroupController {
    @Autowired
    private IRoleGroupService roleGroupService;

    /**
     * 获得列表
     */
    // GET /list
    @ApiOperation(value = "获得列表")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageIndex",
                    dataType = "Int",
                    required = true,
                    value = "当前页，从0开始",
                    defaultValue = "0"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "pageSize",
                    dataType = "Int",
                    required = true,
                    value = "每页数",
                    defaultValue = "10")
    })
    @ApiResponses(@ApiResponse(code = 200, response = RoleGroupResultModel.class, message = "成功"))
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET})
    public MessageBase getList(@RequestParam(value = "pageIndex") int pageIndex,
                               @RequestParam(value = "pageSize") int pageSize) {
        MessageBase result = new MessageBase();
        Page<RoleGroupDto> page = this.roleGroupService.findList(pageIndex, pageSize);
        result.setPageIndex(page.getNumber());
        result.setPageSize(page.getSize());
        result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

        List<RoleGroupResultModel> models = new ArrayList<>();

        page.getContent().forEach(
                dto -> {
                    RoleGroupResultModel model = new RoleGroupResultModel();
                    model.setId(dto.getId());
                    model.setNumber(dto.getNumber());
                    model.setName(dto.getName());
                    model.setContent(dto.getContent());

                    models.add(model);
                });

        result.setData(models);
        return result;
    }

    /**
     * 修改菜单
     */
    // PATCH /list?ids={ids}
    @ApiOperation(value = "修改菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "path",
                    name = "id",
                    dataType = "Int",
                    required = true,
                    value = "id",
                    defaultValue = "1"),
            @ApiImplicitParam(
                    paramType = "query",
                    name = "ids",
                    dataType = "String",
                    required = true,
                    value = "菜单id，用逗号隔开",
                    defaultValue = "1,2,3,4,5")
    })
    @ApiResponses(@ApiResponse(code = 200, response = RoleGroupResultModel.class, message = "成功"))
    @RequestMapping(
            value = {"/menu/{id}"},
            method = {RequestMethod.PATCH})
    public MessageBase patchMenu(@PathVariable int id, @RequestParam(value = "ids") String ids) {
        MessageBase result = new MessageBase();

        boolean b = this.roleGroupService.updateMenuIds(id, ids);
        result.setSuccess(b);

        return result;
    }

    /**
     * 添加角色组
     *
     * @return 状态
     */
    // POST /
    @ApiOperation(value = "添加角色组")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(method = {RequestMethod.POST})
    public MessageBase post(@RequestBody RoleGroupViewModel model) {
        MessageBase result = new MessageBase();

        RoleGroupDto dto = new RoleGroupDto();
        dto.setName(model.getName());
        dto.setContent(model.getContent());

        int id = this.roleGroupService.add(dto);

        result.setSuccess(id > 0);

        return result;
    }

    /**
     * 获得详情
     */
    // GET /{id}
    @ApiOperation(value = "获得详情")
    @ApiResponses(@ApiResponse(code = 200, response = RoleGroupResultModel.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.GET})
    public MessageBase get(@PathVariable int id) {
        MessageBase result = new MessageBase();

        RoleGroupDto dto = this.roleGroupService.find(id);
        RoleGroupResultModel model = new RoleGroupResultModel();
        BeanUtils.copyProperties(dto, model);

        result.setData(model);

        return result;
    }

    /**
     * 编辑
     */
    // PUT /
    @ApiOperation(value = "编辑")
    @ApiResponses(@ApiResponse(code = 200, response = RoleGroupResultModel.class, message = "成功"))
    @RequestMapping(value = {"/{id}"}, method = {RequestMethod.PUT})
    public MessageBase put(@PathVariable int id, @RequestBody RoleGroupViewModel model) {
        MessageBase result = new MessageBase();
        RoleGroupDto dto = new RoleGroupDto();
        dto.setId(id);
        dto.setName(model.getName());
        dto.setContent(model.getContent());
        boolean b = this.roleGroupService.update(dto);
        result.setSuccess(b);

        return result;
    }
}
